package com.ruoyi.web.controller.inventory;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.inventory.domain.TInvSession;
import com.ruoyi.inventory.service.ITInvSessionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 盘点Controller
 * 
 * @author ruoyi
 * @date 2025-09-24
 */
@RestController
@RequestMapping("/system/inventory")
public class TInventoryController extends BaseController
{
    @Autowired
    private ITInvSessionService tInvSessionService;

    /**
     * 查询盘点列表
     */
    @PreAuthorize("@ss.hasPermi('system:inventory:list')")
    @GetMapping("/list")
    public TableDataInfo list(TInvSession tInvSession)
    {
        startPage();
        List<TInvSession> list = tInvSessionService.selectTInvSessionList(tInvSession);
        return getDataTable(list);
    }

    /**
     * 导出盘点列表
     */
    @PreAuthorize("@ss.hasPermi('system:inventory:export')")
    @Log(title = "盘点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TInvSession tInvSession)
    {
        List<TInvSession> list = tInvSessionService.selectTInvSessionList(tInvSession);
        ExcelUtil<TInvSession> util = new ExcelUtil<TInvSession>(TInvSession.class);
        util.exportExcel(response, list, "盘点数据");
    }

    /**
     * 获取盘点详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:inventory:query')")
    @GetMapping(value = "/{sessionNo}")
    public AjaxResult getInfo(@PathVariable("sessionNo") String sessionNo)
    {
        return success(tInvSessionService.selectTInvSessionBySessionNo(sessionNo));
    }

    /**
     * 新增盘点
     */
    @PreAuthorize("@ss.hasPermi('system:inventory:add')")
    @Log(title = "盘点", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TInvSession tInvSession)
    {
        // 生成盘点单号
        if (tInvSession.getSessionNo() == null || tInvSession.getSessionNo().isEmpty()) {
            String sessionNo = "INV" + System.currentTimeMillis();
            tInvSession.setSessionNo(sessionNo);
        }
        // 设置默认状态
        if (tInvSession.getStatus() == null || tInvSession.getStatus().isEmpty()) {
            tInvSession.setStatus("PENDING");
        }
        // warehouse_code 已经由前端提供，无需额外处理
        // 设置默认的盘点人信息（这里可以根据实际需求修改）
        if (tInvSession.getCheckerId() == null) {
            tInvSession.setCheckerId(1L); // 默认盘点人ID
        }
        if (tInvSession.getInventoryUser() == null || tInvSession.getInventoryUser().isEmpty()) {
            tInvSession.setInventoryUser("系统管理员"); // 默认盘点人姓名
        }
        return toAjax(tInvSessionService.insertTInvSession(tInvSession));
    }

    /**
     * 修改盘点
     */
    @PreAuthorize("@ss.hasPermi('system:inventory:edit')")
    @Log(title = "盘点", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TInvSession tInvSession)
    {
        return toAjax(tInvSessionService.updateTInvSession(tInvSession));
    }

    /**
     * 删除盘点
     */
    @PreAuthorize("@ss.hasPermi('system:inventory:remove')")
    @Log(title = "盘点", businessType = BusinessType.DELETE)
	@DeleteMapping("/{sessionNos}")
    public AjaxResult remove(@PathVariable String[] sessionNos)
    {
        return toAjax(tInvSessionService.deleteTInvSessionBySessionNos(sessionNos));
    }

    /**
     * 执行盘点
     */
    @PreAuthorize("@ss.hasPermi('system:inventory:execute')")
    @Log(title = "执行盘点", businessType = BusinessType.UPDATE)
    @PostMapping("/execute")
    public AjaxResult execute(@RequestBody TInvSession tInvSession)
    {
        int result = tInvSessionService.executeInventory(tInvSession);
        if (result > 0) {
            return success("执行盘点成功");
        } else {
            return error("执行盘点失败");
        }
    }
}
